package com.oocl.homework0718.dao.imp;

import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.oocl.homework0718.dao.BookDao;
import com.oocl.homework0718.dbutil.DBUtil;
import com.oocl.homework0718.pojo.Books;


public class BookDaoImp implements BookDao {

	public int addBook(Books b) {
		// TODO Auto-generated method stub
		String sql = "insert into books(id,name,pub_data,author,price,is_new,publisher) values (eq01.nextval,?,?,?,?,?,?)";
		Connection con =null;
		PreparedStatement pst = null;
		int m = 0;
		try {
			con = DBUtil.createConnection();
			pst=con.prepareStatement(sql);
			pst.setString(1, b.getName());
			pst.setDate(2,new java.sql.Date(b.getPublicationDate().getTime()));
			pst.setString(3,b.getAuthor());
			pst.setDouble(4,b.getPrice());
			pst.setBoolean(5,b.getFineness());
			pst.setString(6,b.getPublisher());
			m = pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.close(con,pst,null);
		}
		
		System.out.println(m);
		return m;
	}
	

	public int addBook2(String path) {
		// TODO Auto-generated method stub
		String sql = "insert into books(id,name,pub_data,author,price,is_new,publisher) values (eq01.nextval,?,?,?,?,?,?)";
		Connection con =null;
		PreparedStatement pst = null;
		int m = 0;
		con = DBUtil.createConnection();


			try {
				pst=con.prepareStatement(sql);
				ObjectInputStream in=new ObjectInputStream(new FileInputStream(new File(path))); 

				int i=1;
				Books tempBook;
				while((tempBook = (Books) in.readObject())!=null){
					pst.setString(1, tempBook.getName());
					pst.setDate(2,new java.sql.Date(tempBook.getPublicationDate().getTime()));
					pst.setString(3,tempBook.getAuthor());
					pst.setDouble(4,tempBook.getPrice());
					pst.setBoolean(5,tempBook.getFineness());
					pst.setString(6,tempBook.getPublisher());
					System.out.println(tempBook);
					System.out.println(i++);
					//tempBook = (Books) in.readObject();
				}
					in.close();
			m = pst.executeUpdate();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.close(con,pst,null);
		}
		
		System.out.println(m);
		return m;
	}
	
	
	
	public int deleteBook(Integer id) {
		// TODO Auto-generated method stub
		String sql = "delete books where id =?";
		int m = 0;
		PreparedStatement pst = null;
		Connection con = DBUtil.createConnection();
		try {
			pst=con.prepareStatement(sql);
			pst.setInt(1, id);
			m = pst.executeUpdate();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.close(con,pst,null);
		}
		System.out.println(m);
		return m;
	}

	public int updateBook(Books b) {
		// TODO Auto-generated method stub
		String sql = "update books set name=?,pub_data=?,author =?,price=?,is_new =?,publisher =? where id =?";
		PreparedStatement pst = null;
		int m = 0;
		Connection con = null;
		try {
			con = DBUtil.createConnection();
			pst=con.prepareStatement(sql);
			pst.setString(1, b.getName());
			pst.setDate(2,new java.sql.Date(b.getPublicationDate().getTime()));
			pst.setString(3,b.getAuthor());
			pst.setDouble(4,b.getPrice());
			pst.setBoolean(5,b.getFineness());
			pst.setString(6,b.getPublisher());
			pst.setInt(7,b.getId());
			m = pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.close(con,pst,null);
		}
		return m;
	}

	public Books loadBook(Integer id) {
		// TODO Auto-generated method stub
		Books b =null;
		String sql = "select id,name,pub_data,author,price,is_new,publisher from books";
		PreparedStatement pst = null;
		ResultSet rs = null;
		Connection con =null;
		try {
			con = DBUtil.createConnection();
			pst=con.prepareStatement(sql);
			rs = pst.executeQuery();
			while(rs.next()){
				if(id == rs.getInt("id")){
				String name = rs.getString("name");
				int id1 = rs.getInt("id");
				Date publishDate = new Date(rs.getDate("pub_data").getTime());
				String author = rs.getString("author");
				double price = rs.getDouble("price");
				Boolean fineness = rs.getBoolean("is_new");
				String publisher = rs.getString("publisher");
				b =new Books(id1,name,publishDate,author,price,fineness,publisher);
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.close(con,pst,rs);
		}
		return b;
	}

	public List<Books> findAll() {
		// TODO Auto-generated method stub
		String sql = "select id,name,pub_data,author,price,is_new,publisher from books";
		PreparedStatement pst = null;
		ResultSet rs = null;
		List<Books> list = new ArrayList<Books>();
		Connection con =null;
		Books b;
		try {
		    con = DBUtil.createConnection();
			pst=con.prepareStatement(sql);
			rs = pst.executeQuery();
			while(rs.next()){
				String name = rs.getString("name");
				int id = rs.getInt("id");
				Date publishDate = new Date(rs.getDate("pub_data").getTime());
				String author = rs.getString("author");
				double price = rs.getDouble("price");
				Boolean fineness = rs.getBoolean("is_new");
				String publisher = rs.getString("publisher");
				b =new Books(id,name,publishDate,author,price,fineness,publisher);
				list.add(b);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.close(con,pst,rs);
		}
		return list;
	}

}
